function [SplitedModelFiles, IterList] = splitModelFiles(model_path)
% Mordecai Lee, 2017-07-14, Fri, 22:41:46

if ischar(model_path)
    model_files = dir(fullfile(model_path, '*.caffemodel'));
elseif isstruct(model_path)
    model_files = model_path;
end

if isempty(model_files)
    SplitedModelFiles = {};
    IterList = {};
    return
end

[~, idx] = sort({model_files(:).date});
model_files = {model_files(idx).name};

files = cellfun(@(x) strsplit(x, '_iter_'), model_files, 'uniformoutput', false);
files = [files{:}];
prefixList = files(1 : 2 : end);
suffixList = files(2 : 2 : end);

len = length('.caffemodel');
iterNums = cell2num(cellfun(@(x)str2double(x(1 : end-len)), suffixList, 'uniformoutput', false));

[prefix, ~, ids] = unique(prefixList, 'stable');
SplitedModelFiles = cell(1, length(prefix));
IterList = cell(1, length(prefix));
for id = 1 : length(prefix)
    idx0 = find(ids == id);
    [IterList{id}, idx] = sort(iterNums(idx0));
    idx = idx0(idx);
    SplitedModelFiles{id} = cellfun(@(x)[prefix{id} '_iter_' x], suffixList(idx), 'uniformoutput', false);
end
